<?php
namespace Ucmaim\Controller\Home;

class GoodsController extends CommonController {

    //商品详情
    public function Goods(){
        $id=I('get.id');
        $goods=M('goods');
        $details=$goods
            ->join('uc_goods_category ON uc_goods.gc_id = uc_goods_category.gc_id')
            ->join('uc_goods_type ON uc_goods.goods_typeid = uc_goods_type.type_id')
            ->where("uc_goods.goods_id=%d AND uc_goods.goods_status in (-3,-2,-1,0,1,2,3)",$id)
            ->find();
        if($details['goods_pic']==''){
            $pic=explode(",",$details['gc_relation']);
            $this->pic=M('goods_category')->where("gc_id=$pic[0]")->find();
        }
        $this->gc_id=$goods->where("goods_id=$id")->field("gc_id")->find();
        $son=rtrim($details['gc_relation'],',');
        $map['gc_id'] = array("in",$son);
        $category=M('goods_category');
        $this->server=$category->where($map)->select();
        $this->ku=$details['goods_storage']-$details['goods_freeze'];
        $this->assign('details',$details);

        $img=array();
        $this->img=explode(",",$details['goods_pic']);
        $this->display('Home/Goods');
    }

    //商品列表
    public function Items(){
        $get = I('get.');
        $g = (int)$get['g'];
        $page = (int)$get['Page'];
        $page = $page > 0 ? $page : 1;

        $map['A.goods_status'] = 1;
        $map['A.goods_storage'] = array('exp', ' > A.goods_freeze');

        //分类筛选
        if ($g > 0) {
            $where['C.gc_relation'] = array('LIKE', '%' . $g . '%');
            $where['C.gc_id'] = $g;
            $where["_logic"] = 'or';
            $map['_complex'] = $where;

            //面包屑导航
            $nav = D('GoodsCategory')->getOne($g);
            if (!empty($nav)) {
                $nav = D('GoodsCategory')->getRelation($nav);
                $this->assign('nav', $nav);
            }
        }

        //关键词筛选
        if ($get['keyword'] != '') {
            $map['A.goods_title'] = array('LIKE', '%' . $get['keyword'] . '%');
        }

        //类型筛选
        if ($get['type'] != '') {
            $map['B.type_id'] = $get['type'];
        }

        //由价格低到高排序
        $order = '';
        if ($get['sort'] == 1) {
            $order = 'A.goods_price asc';
        }

        $list = D('Goods')->getAll($page, 10, $show_page, $map, $order);
        $this->assign('show_page', $show_page);
        $this->assign('list', $list);
        $this->display('Home/Items');
    }



}
